/*
 * @Author: toString muzi_li0323@163.com
 * @Date: 2024-05-22 16:44:13
 * @LastEditors: toString
 * @LastEditTime: 2025-01-13 18:33:16
 * @FilePath: \token-for-token-admin\src\require\require.js
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
import axios from "axios";
import { ElMessage } from "element-plus";
import { useUserStore } from "@/stores";
const userStore = useUserStore();
// const router = useRouter();
// const baseURL = 'http://192.168.0.116:8848/api';
// const baseURL = 'http://localhost:8848/api';
// const baseURL = 'https://vip.skysp.info/api';
const baseURL = import.meta.env.VITE_BASE_URL + '/admin/';
// const baseURL = 'https://clapi.wwwrfs.com/api'; // 最新域名

const ins = axios.create({
    baseURL,
    timeout: 5000,
});


// 设置 请求拦截
ins.interceptors.request.use((config) => {
    config.headers.language = userStore.getLocale;

    let token = userStore.getToken;
    if (token) {
        config.headers.Authorization = token
    }

    return config;
}, (err) => {
    return Promise.reject(err);
});

// 设置响应拦截
ins.interceptors.response.use((res) => {
    // console.log('响应拦截');
    if(res.data.code === 401){
        userStore.setToken('');
        window.location.href = '/login';
        return
    } else if(res.data.code === 200){
        return res.data
    } else {
        ElMessage.error(res.data.msg);
        return;
    }
}, (err) => {
    return Promise.reject(err);
})

export default ins;
